package com.mdp.im.core.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.utils.BaseUtils;
import com.mdp.im.core.entity.ImGroupUser;
import com.mdp.im.core.service.cache.ImGroupCacheService;
import com.mdp.im.core.vo.GroupUserVo;
import com.mdp.im.core.vo.GroupVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mdp.core.service.BaseService;
import static com.mdp.core.utils.BaseUtils.*;
import com.mdp.core.entity.Tips;
import com.mdp.core.err.BizException;

import com.mdp.im.core.entity.ImGroup;
import com.mdp.im.core.mapper.ImGroupMapper;
import org.springframework.util.StringUtils;

/**
 * @author 唛盟开源 code-gen
 * @since 2024-8-13
 */
@Service
public class ImGroupService extends BaseService<ImGroupMapper,ImGroup> {
	static Logger logger =LoggerFactory.getLogger(ImGroupService.class);

	/**
	 * 自定义查询，支持多表关联
	 * @param page 分页条件
	 * @param ew 一定要，并且必须加@Param("ew")注解
	 * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
	 * @return
	 */
	public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
		return baseMapper.selectListMapByWhere(page,ew,ext);
	}


	@Autowired
	ImGroupUserService groupUserService;
	@Autowired
	ImGroupCacheService groupCacheService;


	public ImGroup getImGroupByGroupId(String groupId){
		ImGroup group=groupCacheService.getGroup(groupId);
		if(group==null) {
			group=getImGroupByGroupId(groupId);
			groupCacheService.putGroup(groupId, group);
		}
		return group;

	}

	public void clearCahceGroup(String groupId) {
		groupCacheService.putGroup(groupId, null);
	}

	public void putGroupToCache(String groupId, ImGroup group) {
		groupCacheService.putGroup(groupId, group);
	}

	public void asyncCreateGroupAndBatchInsertUsers(ImGroup group, List<ImGroupUser> groupUsers) {
		if(StringUtils.isEmpty(group.getId())) {
			group.setId(super.createKey("id"));
			super.insert(group);
		}else {
			super.insertOrUpdate(group);
		}
		if(groupUsers!=null && groupUsers.size()>0) {
			groupUserService.batchInsertOrUpdate(group.getId(),groupUsers);
		}
	}
	public void asyncBatchJoinGroupUsers(ImGroup group, List<ImGroupUser> groupUsers) {
		if(groupUsers!=null && groupUsers.size()>0) {
			groupUserService.batchInsertOrUpdate(group.getId(),groupUsers);
		}
	}
	public void asyncBatchLeaveGroupByGroupIdAndUserids(String groupId, List<ImGroupUser> groupUsers) {
		groupUserService.batchLeaveGroupByGroupIdAndUserids(groupId ,groupUsers);

	}
	//查询我所在的群,不关联聊天
	public List<Map<String,Object>> listMyGroupsOnly(String branchId, String userid) {
		Map<String,Object> map =new HashMap<>();
		map.put("branchId", branchId);
		map.put("userid", userid);
		return baseMapper.listMyGroups(map);
	}
	//查询我所在的群,关联聊天
	public List<Map<String,Object>> listMyGroups(String branchId, String userid) {
		// TODO Auto-generated method stub
		Map<String,Object> map =new HashMap<>();
		map.put("branchId", branchId);
		map.put("userid", userid);
		return baseMapper.listMyGroupsAndNewMessage(map);
	}
	//查询我所在的群,关联聊天
	public List<Map<String,Object>> listMyGroups(String branchId, String userid,String lastLoadImDataTime) {
		// TODO Auto-generated method stub
		Map<String,Object> map =new HashMap<>();
		map.put("branchId", branchId);
		map.put("userid", userid);
		map.put("lastLoadImDataTime",lastLoadImDataTime);
		return baseMapper.listMyGroupsAndNewMessage(map);
	}

	public GroupVo queryGroupAndUsers(String branchId, String groupId) {
 		ImGroup imGroup=super.getById(groupId);
		GroupVo group= BaseUtils.fromMap(BaseUtils.toMap(imGroup), GroupVo.class);
		group.setGroupId(group.getId());
		List<GroupUserVo> users=groupUserService.listGroupUsers(groupId);
		group.setUsers(users);
		return group;
	}
}

